package com.devops.controller;

import com.devops.entity.req.CmdReq;
import com.devops.entity.req.SshInfo;
import com.devops.entity.resp.ResponseResult;
import com.devops.service.SshService;
import com.devops.utils.ResponseUtils;
import com.devops.utils.error.ReturnErrorException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ssh")
@Tag(name  = "ssh连接")
public class SshController {

    @Autowired
    private SshService sshService;

    @GetMapping("/list")
    @Operation(summary = "ssh连接全部信息")
    public ResponseResult getConnect() throws ReturnErrorException {
        return ResponseUtils.success(sshService.getSshConnect());
    }

    @PostMapping("/connect")
    @Operation(summary = "ssh连接上")
    public ResponseResult<String> connect(@RequestBody SshInfo sshInfo) throws ReturnErrorException {
        return ResponseUtils.success(sshService.SshConnect(sshInfo));
    }

    @PostMapping("/select")
    @Operation(summary = "查询连接")
    public ResponseResult select(@RequestBody SshInfo sshInfo) throws ReturnErrorException {
        return ResponseUtils.success(sshService.getSshConnect(sshInfo.getHostIp()));
    }

    @PostMapping("/delete")
    @Operation(summary = "删除连接")
    public ResponseResult delete(@RequestBody SshInfo sshInfo) throws ReturnErrorException {
        sshService.deleteConnect(sshInfo.getHostIp());
        return ResponseUtils.success("success");
    }

    @PostMapping("/cmd")
    @Operation(summary = "执行命令")
    public ResponseResult cmd(@RequestBody CmdReq cmdReq) throws Exception {
//        sshService.deleteConnect(sshInfo.getHostIp());
        return ResponseUtils.success(sshService.cmd(cmdReq));
    }

    @PostMapping("ces")
    @Operation(summary = "ces")
    public ResponseResult ces(@RequestBody CmdReq cmdReq) throws Exception {
        sshService.cmd(cmdReq.getHostIp(), cmdReq.getCmd());
        return ResponseUtils.success("success");
    }

}
